package com.yjt.fresh.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpVpFrg;
import com.yjt.fresh.api.ApiGetInformationTypeList;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanInformationType;
import com.yjt.fresh.constant.MyConstants;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class FrgInformation extends BaseSimpleFragment {


    @BindView(R.id.indicatorAppFrgInformation)
    MagicIndicator mIndicator;
    @BindView(R.id.vpAppFrgInformation)
    ViewPager mVp;

    private CommonNavigator mCommonNavigator;
    private CommonNavigatorAdapter mCommonNavigatorAdapter;

    @Override
    protected int getContentView() {
        return R.layout.app_atymain_frg_infopage;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);
    }


    @Override
    protected void initEvents() {
        getData();
        initClick();
    }


    private void getData() {
        ApiGetInformationTypeList apiGetInformationTypeList = new ApiGetInformationTypeList(new HttpOnNextListener<List<BeanInformationType>>() {


            @Override
            public void onNext(List<BeanInformationType> beanInformationTypes) {
                initIndicator(beanInformationTypes);
                initVp(beanInformationTypes);
                ViewPagerHelper.bind(mIndicator, mVp);
            }

            @Override
            public void onReLogin() {

            }
        }, mContext, true, false, false);
        HttpManager.getInstance().doHttpDeal(apiGetInformationTypeList);
    }

    private void initVp(List<BeanInformationType> beanInformationTypes) {

        ArrayList<BaseSimpleFragment> frgList = new ArrayList<>();
        for (int i = 0; i < beanInformationTypes.size(); i++) {
            FrgInformationList frgInformationList = new FrgInformationList();
            Bundle bundle = new Bundle();
            bundle.putString(MyConstants.INFO_TYPE, beanInformationTypes.get(i).getARTICLETYPE());
            frgInformationList.setArguments(bundle);
            frgList.add(frgInformationList);
        }
        AdpVpFrg adpVpFrg = new AdpVpFrg(getFragmentManager(), frgList);
        mVp.setAdapter(adpVpFrg);
        mVp.setOffscreenPageLimit(0);
    }

    private void initIndicator(final List<BeanInformationType> beanInformationTypes) {
        mIndicator.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        mCommonNavigator = new CommonNavigator(mContext);
        mCommonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return beanInformationTypes.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(mContext.getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(mContext.getResources().getColor(R.color.color_43b624));
                simplePagerTitleView.setText(beanInformationTypes.get(i).getARTICLETYPE());
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mVp.setCurrentItem(i, false);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(mContext.getResources().getColor(R.color.color_009944));
                return linePagerIndicator;
            }
        };
        mCommonNavigator.setAdapter(mCommonNavigatorAdapter);
        mIndicator.setNavigator(mCommonNavigator);
    }

    private void initClick() {

    }

}
